package com.example.mq.demo;

import com.example.mq.common.MqException;
import com.example.mq.mqclient.Channel;
import com.example.mq.mqclient.Connection;
import com.example.mq.mqclient.ConnectionFactory;
import com.example.mq.mqserver.core.BasicProperties;
import com.example.mq.mqserver.core.Consumer;
import com.example.mq.mqserver.core.ExchangeType;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Slf4j
public class DemoConsumer {
    public static void main(String[] args) throws IOException, MqException, InterruptedException {
        log.info("启动消费者");
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(9090);

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("testExchange", ExchangeType.DIRECT, true, false, null);
        channel.queueDeclare("testQueue", true, false, false, null);

        channel.basicConsume("testQueue", true, new Consumer() {
            @Override
            public void handleDelivery(String consumerTag, BasicProperties basicProperties, byte[] body) throws MqException, IOException {
                log.info("开始消费数据");
                log.info("consumerTag :{}", consumerTag);
                log.info("basicProperties:{}", basicProperties);
                String bodyString = new String(body, 0, body.length);
                log.info("body:{}", bodyString);
                log.info("消费数据结束");
            }
        });
        while (true){
            Thread.sleep(500);
        }
    }

}
